<template>
  <div class="dz">
    <span>还可以的话就点个赞吧!</span>
    <div>
      <label for="checkbox" title="谢谢您~" >
        <input type="checkbox" id="checkbox" hidden>
        <svg t="1651108144758" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
            p-id="3464" >
            <path
                d="M512 896a42.666667 42.666667 0 0 1-30.293333-12.373333l-331.52-331.946667a224.426667 224.426667 0 0 1 0-315.733333 223.573333 223.573333 0 0 1 315.733333 0L512 282.026667l46.08-46.08a223.573333 223.573333 0 0 1 315.733333 0 224.426667 224.426667 0 0 1 0 315.733333l-331.52 331.946667A42.666667 42.666667 0 0 1 512 896z"
                p-id="3465" id="heart"></path>
        </svg>
      </label>
    </div>
    <div class="dz-count" title="当前点赞的帅哥靓女数">
      {{
        likeCount
      }}
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const likeCount = ref(0);
const getLike = async() => {
	const res = await fetch(`http://1.95.157.217/api/map/getLikeCount`, {
		method: 'GET'
	}).then(response => {
		if (!response.ok) {
				throw new Error(`HTTP error! status: ${response.status}`);
		}
		return response.json();
})
	if (res.code === 0) {
		likeCount.value = res.data;
	}
}
const sendLike = async() => {
	const res = await fetch('http://1.95.157.217/api/map/sendLike', {
		method: 'POST',
    body: JSON.stringify({
      page_from: 'mapDoc'
    }),
    headers: {
      'Content-Type': 'application/json'
    }
	}).then(response => {
		if (!response.ok) {
				throw new Error(`HTTP error! status: ${response.status}`);
		}
	})
}
onMounted(() => {
  getLike()
  const heart = document.querySelector('#heart')
  const checkbox = document.querySelector('#checkbox')
  heart.addEventListener('click', async() => {
    if (!checkbox.checked) {
      await sendLike()
      await getLike()
    }
  })
})
onUnmounted(() => {
  heart.removeEventListener('click', () => {});
})
</script>
<style scoped>
.dz {
  display: inline-flex;
  align-items: center;
  color: #fff;
}
.dz span {
  margin-right: 10px;
}
.dz div {
  height: 37px;
  line-height: 37px;
  width: 50px;
  text-align: center;
}
svg{
  width: 37px;
  position: relative;
  z-index: 0;
}
#heart{
    fill:#eee;
    /* stroke 属性可应用于任何种类的线条，文字和元素，就像一个
    圆的轮廓 */
    stroke:var(--jjext-color-tips);
    /* 线条宽度 */
    stroke-width: 40px;
    /* 设置线条为虚线，虚线的长度 */
    stroke-dasharray: 2600;
    /* 线条的位移 */
    stroke-dashoffset: 2600;
    /* 端点为圆头 */
    stroke-linecap: round;
}
#checkbox:checked + svg #heart{
    animation: drawHeart 1s linear forwards;
}
label{
    cursor: pointer;
}
#checkbox:checked + svg{
    animation: beat 1s linear forwards;
}
@keyframes drawHeart{
    0%{
        stroke-dashoffset: 2600;
    }
    80%{
        fill:#eee;
        stroke-dashoffset: 0;
    }
    100%{
        fill: var(--jjext-color-tips);
        stroke-dashoffset: 0;
    }
}
@keyframes blink {
    0%{
        transform: translate(-50%,-50%) scale(0.5);
        opacity: 0.8;
    }
    50%{
        transform: translate(-50%,-50%) scale(1);
        opacity: 1;
    }
    100%{
        transform: translate(-50%,-50%) scale(1.1);
        opacity: 0;
    }
}
/* 心跳动画 */
@keyframes beat{
    0%{
        transform: scale(1);
    }
    70%{
        transform: scale(1);
    }
    80%{
        transform: scale(1.2);
    }
    100%{
        transform: scale(1);
    }
}
</style>